
(function () {
    'use strict';

    angular.module('KingAdmin.pages.pdev.devopt')
        .controller('DevoptCtrl', DevoptCtrl);

    /** @ngInject */
    function DevoptCtrl($scope, $filter, $interval, $stateParams, $state,$uibModalInstance, DevoptService, DictService, DeviceService) {
        var kt = this;
        kt.isAdd = true;
        kt.title = '设备指令控制';
        kt.devopt = {
            optStatus: '0'
        };

        kt.optDevList = DeviceService.getOptDevList();
        kt.devstatus = DictService.getDictMap('DEVICESTATUS');
        kt.devMsgType = DictService.getDictMap('DEV_MSG_TYPE');
        kt.devOptType = DictService.getDictMap('DEV_OPT_TYPE');
        kt.getDictLabel = DictService.getDictLabel;

        kt.save = function () {
            DevctrlService.save(kt.devctrl,function (data) {
                $uibModalInstance.close(true);
            });
        };

        $uibModalInstance.rendered.then(function() {

        });

        $scope.$watch('kt.devopt.deviceUid', function (value) {
            if (!value) {
                return;
            }

            DeviceService.getByUid({uid: value}, function (data) {
                kt.devopt.isOnline = data.isOnline;
            });
        });

        $scope.$watch('kt.devopt.optType', function (value) {
            console.log(value);

            var selected = [];
            if (kt.devOptType && kt.devOptType.length > 0) {
                if (value || value == 0) {
                    selected = $filter('filter')(kt.devOptType, {value: value});
                }
                console.log('optType');
                console.log(selected);
                if (selected.length > 0) {
                    kt.devopt.optBodyLen = parseInt(selected[0].remarks) * 2;
                    kt.devopt.msgLen = 15 + parseInt(selected[0].remarks);
                }
            }
        });

        $scope.$watch('kt.devopt.optMsg', function (value) {
            // 设置指令预览
            if (value && value.length == kt.devopt.optBodyLen) {

                var msg = 'AA' +
                    kt.devopt.msgType +
                    intToHex(kt.devopt.msgLen, 4) +
                    kt.devopt.deviceUid +
                    kt.devopt.optType +
                    intToHex(parseInt(kt.devopt.optBodyLen / 2, 16), 2) +
                    value +
                    '0000' +
                    'BB';

                kt.devopt.msg = msg.toUpperCase();
                kt.devopt.id = '';
            } else {
                kt.devopt.msg = null;
            }
        });

        $scope.$watch('kt.devopt.msg', function (value) {
            if (value) {
                kt.devopt.msgLen = parseInt(value.length) / 2;
                kt.devopt.msgCLen = parseInt(value.length);
            }
        });

        function intToHex(val, len) {
            var hexStr = val.toString(16);
            var zeroStr = '';

            for (var i=0; i < len - hexStr.length; i++) {
                zeroStr += '0';
            }
            return zeroStr + hexStr;
        }

        kt.timer = null;

        kt.sendMsg = function () {
            kt.devopt.optStatus = '1';

            // 如果有定时器，取消定时器
            if (kt.timer) {
                $interval.cancel(kt.timer);
            }

            kt.devopt.retMsg = '';
            kt.devopt.retMsgLen = '';
            kt.devopt.retTime = '';
            kt.devopt.id = '';

            DevoptService.sendMsg(kt.devopt,function (data) {
                console.log('send Msg');
                console.log(data);
                kt.devopt.id = data.result.id;
                // 进行定时器设置操作
                kt.timer = $interval(getRetMsg, 1500);
            });
        };

        // 获取返回数据
        function getRetMsg() {
            if (!kt.devopt.id && kt.timer) {
                $interval.cancel(kt.timer);
            }

            DevoptService.getInfo({id:kt.devopt.id},
                function (data) {
                    if (data.optStatus == '2') {
                        kt.devopt.retMsg = data.recvMsg;
                        // kt.devopt.retTime = data.recvTime;
                        if (kt.devopt.retMsg.length > 0) {
                            kt.devopt.retMsgLen = kt.devopt.retMsg.length / 2 + 4;

                            var createTime = data.createTime;
                            var recvTime = data.recvTime;

                            var ctsp = Date.parse(new Date(createTime)) ;
                            var rtsp = Date.parse(new Date(recvTime)) ;

                            kt.devopt.retTime = (rtsp - ctsp) / 1000.0;
                        }

                        // 清除定时器
                        if (kt.timer) {
                            $interval.cancel(kt.timer);
                        }
                    }
                });
        }

        // 离开页面清除timer
        $scope.$on("$destroy", function() {
            if (kt.timer) {
                $interval.cancel(kt.timer);
            }
        })
    }

})();
